package 第2节数组;

import java.util.Arrays;
import java.util.Scanner;

public class LianXi01 {
    public static void main(String[] args) {
/*
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int target = input.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = input.nextInt();
        }
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] + arr[j] == target) {
                    System.out.println(i + " " + j);
                    break;
                }
            }
        }
*/
        //如果给定的数组本身就有序 O(n*logn)
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int target = 21;
        for (int i = 0; i < arr.length - 1; i++) {
            int low = i + 1;
            int high = arr.length - 1;
            int mid = (low + high) / 2;
            int key = target - arr[i];
            if (key > arr[high] || key < arr[low]) {
                continue;
            }
            while (arr[mid] != key) {
                if (arr[mid] < key) {
                    low = mid + 1;
                } else {
                    high = mid - 1;
                }
                if (high < low) {
                    mid = -1;
                    break;
                }
                mid = (low + high) / 2;
            }
            if (mid != -1) {
                System.out.println(i + " " + mid);
                break;
            }
        }
    }
}
